package chatServer

import (
	"net"
	"fmt"
)

type Server struct {
	rooms    map[string]*Room
	port     int
	maxRoom  int
	maxAgent int
}

func (s *Server)initRooms() {
	s.rooms = make(map[string]*Room)
	s.rooms["_root"] = NewRoom()
}

func (s *Server) Login() {

}

func NewServer() (server *Server) {
	server = &Server{
		rooms:make(map[string]*Room),
		port:2345,
		maxRoom:30,
		maxAgent:99,
	}
	server.Start()
	return
}

func (s *Server) Start() {

	tcpAddr, _ := net.ResolveTCPAddr("tcp", ":1234")
	listener, _ := net.ListenTCP("tcp", tcpAddr)
	fmt.Println("server start.......")
	s.initRooms()
	for {
		conn, _ := listener.Accept()
		fmt.Println("accept new connection")
		go s.handleConn(conn)
	}
}

func (s *Server) handleConn(conn net.Conn) {
	NewAgent(conn, s.rooms["_root"])

}